package com.woparker.kiosk.utils;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;


import com.woparker.kiosk.base.ContextHolder;

import java.util.Locale;

/**
 * 多语言切换的帮助类
 */
public class MultiLanguageUtil {
    public static final int LANGUAGE_FOLLOW_SYSTEM = 0; //跟随系统
    public static final int LANGUAGE_EN = 1;    //英文
    public static final int LANGUAGE_CHINESE_SIMPLIFIED = 2; //简体
    public static final int LANGUAGE_CHINESE_TRADITIONAL = 3;  //香港台湾繁体
    public final static String SERVER_ZH_CN = "zh_CN";
    public final static String SERVER_EN_US = "en_US";
    public static final String LOCALE_EN = "en";
    public static final String LOCALE_ZH_CN = "zh_CN";
    public static final String LOCALE_ZH_HK = "zh_HK";
    public static final String LOCALE_ZH_TW = "zh_TW";

    public static final String CONFIG_INFO = "config_info";

    public static final String CONFIG_INFO_TWO = "config_info_two";
    private static final String TAG = "MultiLanguageUtil";
    private static volatile MultiLanguageUtil instance;
    private Context mContext;
    public static final String SAVE_LANGUAGE = "save_language";

    public static void init(Context mContext) {
        if (instance == null) {
            synchronized (MultiLanguageUtil.class) {
                if (instance == null) {
                    instance = new MultiLanguageUtil(mContext);
                }
            }
        }
    }

    public static MultiLanguageUtil getInstance() {
        if (instance == null) {
            synchronized (MultiLanguageUtil.class) {
                if (null == instance) {
                    instance = new MultiLanguageUtil(ContextHolder.getAppContext());
                }
            }
        }
        return instance;
    }

    private MultiLanguageUtil(Context context) {
        this.mContext = context;
    }

    /**
     * 设置语言
     */
    public void setConfiguration() {

//        Configuration configuration = mContext.getResources().getConfiguration();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            configuration.setLocale(targetLocale);
//        } else {
//            configuration.locale = targetLocale;
//        }
//        setRequestLanguage(targetLocale);
//        Resources resources = mContext.getResources();
//        DisplayMetrics dm = resources.getDisplayMetrics();
//        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!


        Locale locale = getLanguageLocale();
        Resources resources = mContext.getResources();
        Configuration configuration = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mContext.createConfigurationContext(configuration);
        } else {
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
    }

    public void setRequestLanguage(Locale locale) {
        String language = getSystemLanguage(locale);
        if (TextUtils.equals(language, LOCALE_ZH_CN)) {
            setLanguage(LOCALE_ZH_CN);
        } else if (TextUtils.equals(language, LOCALE_ZH_TW)) {
            setLanguage(LOCALE_ZH_CN);
            put(CONFIG_INFO_TWO, LOCALE_ZH_TW);
        } else {
            setLanguage(LOCALE_EN);
        }
    }

    /**
     * 如果不是英文、简体中文、繁体中文，默认返回简体中文
     *
     * @return
     */
    private Locale getLanguageLocale() {
        int languageType = getInt(MultiLanguageUtil.SAVE_LANGUAGE, 0);
        if (languageType == LANGUAGE_FOLLOW_SYSTEM) {
            Locale sysLocale = getSysLocale();
            setRequestLanguage(sysLocale);
            return sysLocale;
        } else if (languageType == LANGUAGE_EN) {
            return Locale.ENGLISH;
        } else if (languageType == LANGUAGE_CHINESE_SIMPLIFIED) {
            return Locale.SIMPLIFIED_CHINESE;
        } else if (languageType == LANGUAGE_CHINESE_TRADITIONAL) {
            return Locale.TRADITIONAL_CHINESE;
        }
        getSystemLanguage(getSysLocale());
        Log.e(TAG, "getLanguageLocale" + languageType + languageType);
        return Locale.SIMPLIFIED_CHINESE;
    }

    public static String getSystemLanguage(Locale locale) {
        return locale.getLanguage() + "_" + locale.getCountry();

    }

    //以上获取方式需要特殊处理一下
    public Locale getSysLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    /**
     * 更新语言
     *
     * @param languageType
     */
    public void updateLanguage(int languageType) {
        putInt(MultiLanguageUtil.SAVE_LANGUAGE, languageType);
        setConfiguration();
        if (languageType == LANGUAGE_CHINESE_SIMPLIFIED) {
            setLanguage(LOCALE_ZH_CN);
        } else if (languageType == LANGUAGE_FOLLOW_SYSTEM) {
            Locale sysLocale = getSysLocale();
            setRequestLanguage(sysLocale);
        } else if (languageType == LANGUAGE_CHINESE_TRADITIONAL) {
            setLanguage(LOCALE_ZH_TW);
        } else if (languageType == LANGUAGE_EN) {
            setLanguage(LOCALE_EN);
        } else {
            setLanguage(LOCALE_EN);
        }
    }

    /**
     * 设置语言
     */
    public void updateLanguage(Locale targetLocale) {
        Configuration configuration = mContext.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(targetLocale);
        } else {
            configuration.locale = targetLocale;
        }
        setRequestLanguage(targetLocale);
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
    }

    /**
     * 获取到用户保存的语言类型
     *
     * @return
     */
    public int getLanguageType() {
        int languageType = getInt(MultiLanguageUtil.SAVE_LANGUAGE, LANGUAGE_FOLLOW_SYSTEM);
        if (languageType == LANGUAGE_CHINESE_SIMPLIFIED) {
            return LANGUAGE_CHINESE_SIMPLIFIED;
        } else if (languageType == LANGUAGE_CHINESE_TRADITIONAL) {
            return LANGUAGE_CHINESE_TRADITIONAL;
        } else if (languageType == LANGUAGE_FOLLOW_SYSTEM) {
            return LANGUAGE_FOLLOW_SYSTEM;
        } else if (languageType == LANGUAGE_EN) {
            return LANGUAGE_EN;
        }
        return languageType;
    }

    public static Context attachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationResources(context);
        } else {
            MultiLanguageUtil.getInstance().setConfiguration();
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context createConfigurationResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getInstance().getLanguageLocale();
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }


    public static void setLanguage(String temp) {
        if (LOCALE_ZH_CN.equalsIgnoreCase(temp)) {
            put(CONFIG_INFO, SERVER_ZH_CN);
            put(CONFIG_INFO_TWO, SERVER_ZH_CN);
        } else if (LOCALE_ZH_HK.equalsIgnoreCase(temp) || LOCALE_ZH_TW.equalsIgnoreCase(temp)) {
            put(CONFIG_INFO, LOCALE_ZH_TW);
            put(CONFIG_INFO_TWO, LOCALE_ZH_TW);
        } else if (LOCALE_EN.equalsIgnoreCase(temp)) {
            put(CONFIG_INFO, LOCALE_EN);
            put(CONFIG_INFO_TWO, LOCALE_EN);
        } else {
            put(CONFIG_INFO, SERVER_EN_US);
            put(CONFIG_INFO_TWO, SERVER_EN_US);
        }

    }


    public static void put(String key, String value) {
        SharedPreferencesUtil.getInstance(ContextHolder.getAppContext()).put(key, value);
    }

    public static int getInt(String key, int defValue) {
        return SharedPreferencesUtil.getInstance(ContextHolder.getAppContext()).getInt(key, defValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences.Editor edit = SharedPreferencesUtil.getInstance(ContextHolder.getAppContext()).getEditor();
        edit.putInt(key, value);
        edit.apply();
    }
}
